/*
 *  UI_VolPanel.cpp
 *
 *	Date: 2011-4-29
 *  Author: linshenqi
 */

#include "stdafx.h"
#include "./UI_Frame.h"
#include "./CP9550_Protocol.h"
#include "./FaderDlg.h"
#include <list>

extern CDC *g_pDC;
extern CDC g_MemDC;

extern CPaintDC *g_pPaintDC;
extern CDC g_MemPaintDC;

extern std::list<GraphicBase *> g_GBList;

UI_VolPanel::UI_VolPanel(const CRect *pRect, const GRAPHICPROP *pProp)
	: GraphicBase(pRect, pProp)
	, m_nMode(CHN_INPUT)
	, m_bFlag(false)
	, m_clrFore(COLOR_FORE)
	, m_bDlg(false)
	, m_Name("")
	, m_nGroup(-1)
	, m_clrBack(COLOR_FORE)
	, m_bDD(false)
{
	//m_FMP.SetParentUI(this);
}

void UI_VolPanel::Redraw()
{
	this->m_pParent->Redraw();
	//this->DrawBmp(this->m_pRect, &this->m_pBmp, &::g_MemDC, ::g_pDC);
	if(m_nMode == CHN_INPUT)
	{
		//m_FMP.Redraw();
	}
}

void UI_VolPanel::ToggleMute(int nFlag)
{
	//if(!this->m_pParent)
		//return;

	if(nFlag == 0)
	{
		m_clrBack = COLOR_FORE;
	}
	else
	{
		m_clrBack = COLOR_MUTE;
	}

	GRAPHICPROP prop;
	prop.clrFill = m_clrBack;
	prop.clrFrame = m_clrBack;
	prop.FrameWidth = 3;
	this->DrawRoundRect(this->m_pRect, &prop, CPoint(12, 12), ::g_pDC, ::g_pDC);
	this->m_VS.Redraw();
}

bool UI_VolPanel::OnPaint(CPaintDC *pDC, CDC *pMemDC)
{
	CPaintDC dc(this->m_pWnd);
	this->SetDC(&dc);
	
	GRAPHICPROP prop;
	prop.clrFill = m_clrBack;
	prop.clrFrame = m_clrBack;
	this->DrawRoundRect(this->m_pRect, &prop, CPoint(12, 12), pDC, pDC);

	prop.clrFrame = m_clrBack;
	prop.FrameWidth = 3;
	this->DrawTransparentRect(this->m_pRect, &prop, pDC, pDC);
	/*if(m_nMode == CHN_INPUT)
	{
		m_FMP.OnPaint(pDC, pMemDC);
	}*/
	//m_FMP.OnPaint(pDC, pMemDC);
	m_VS.OnPaint(pDC, pMemDC);
	GraphicBase::OnPaint(pDC, pMemDC);
	return true;
}

bool UI_VolPanel::OnLButtonDown(UINT nFlags, CPoint point)
{
	CRect rc;
	if(this->m_pWnd->GetDlgItem(this->m_nID + IDD_VOLEDIT1 + 8 * this->m_pParent->GetID())->GetSafeHwnd() == ::GetFocus())
	{
		this->m_pWnd->GetDlgItem(this->m_nID + IDD_VOLEDIT1 + 8 * this->m_pParent->GetID())->GetWindowRect(&rc);
		//this->m_pWnd->ScreenToClient(&point);
		if(!rc.PtInRect(point) && !this->m_VS.m_rcBmp.PtInRect(point))
		{
			m_VS.SetValue(true);
		}
	}

	rc.SetRect(this->m_pRect->left, this->m_pRect->top, this->m_pRect->right, this->m_pRect->bottom + 36);
	if(!rc.PtInRect(point))
		return false;

	m_bDD = true;
	//this->SetDC(this->m_pWnd->GetDC());
	//this->DrawBmp(this->m_pRect, this->m_pBmp);
	//if(m_nMode == CHN_INPUT)
	//{
		//m_FMP.OnLButtonDown(nFlags, point);
	//}
	
	if(m_VS.OnLButtonDown(nFlags, point))
		return true;

	GraphicBase::OnLButtonDown(nFlags, point);
	return true;
}

bool UI_VolPanel::OnLButtonUp(UINT nFlags, CPoint point)
{
	//if(m_nMode == CHN_INPUT)
	//{
		//m_FMP.OnLButtonUp(nFlags, point);
	//}
	m_VS.OnLButtonUp(nFlags, point);
	m_bDD = false;
	GraphicBase::OnLButtonUp(nFlags, point);
	return true;
}

bool UI_VolPanel::OnRButtonDown(UINT nFlags, CPoint point)
{
	if(!this->m_pRect->PtInRect(point))
		return false;

	m_bDlg = true;
	return true;
}

bool UI_VolPanel::OnRButtonUp(UINT nFlags, CPoint point)
{
	if(!this->m_pRect->PtInRect(point) || !m_bDlg || m_bDD)
		return false;

	FaderDlg dlg(this);
	dlg.DoModal();
	m_bDlg = !m_bDlg;
	return true;
}

bool UI_VolPanel::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	CRect rc;
	rc.SetRect(this->m_pRect->left, this->m_pRect->top, this->m_pRect->right, this->m_pRect->bottom + 36);
	if(!rc.PtInRect(point))
		return false;

	m_VS.OnLButtonDblClk(nFlags, point);
	return true;
}

void UI_VolPanel::ToggleEx(int nFlag)
{
	GRAPHICPROP prop;
	prop.FrameWidth = 3;
	if(nFlag == 1)
	{
		prop.clrFrame = COLOR_FORE_MOUSEIN;
	}
	else
	{
		prop.clrFrame = m_clrBack;
	}
	this->DrawTransparentRect(this->m_pRect, &prop, ::g_pDC, ::g_pDC);
}

bool UI_VolPanel::OnMouseMove(UINT nFlags, CPoint point)
{
	//this->SetDC(this->m_pWnd->GetDC());
	GraphicBase *pGB[6];
	int i = 0;
	for(std::list<GraphicBase *>::iterator it = ::g_GBList.begin(); 
		it != ::g_GBList.end(); ++it)
	{
		pGB[i++] = *it;
	}

	UI_InputPanel *pOP = (UI_InputPanel *)pGB[1];
	UI_MixPanel *pMP = (UI_MixPanel *)pGB[2];

	GRAPHICPROP prop;
	prop.FrameWidth = 3;
	if(this->m_pRect->PtInRect(point))
	{
		if(!m_bFlag)
		{
			m_clrFore = COLOR_FORE_MOUSEIN;
			m_bFlag = true;
			prop.clrFrame = m_clrFore;
			this->DrawTransparentRect(this->m_pRect, &prop, ::g_pDC, ::g_pDC);
			if(this->m_pParent->GetID() == 0)
			{
				for(int i = 0; i < 8; ++i)
				{
					if(pMP->m_pFMP[this->GetID()]->m_pTB[i]->m_bBmp)
					{
						pOP->m_VP[i].DrawTransparentRect(pOP->m_VP[i].m_pRect, &prop, ::g_pDC, ::g_pDC);
					}
				}
			}
		}
	}
	else if(!this->m_pRect->PtInRect(point))
	{
		if(m_bFlag)
		{
			m_clrFore = m_clrBack;
			m_bFlag = false;	
			prop.clrFrame = m_clrFore;
			this->DrawTransparentRect(this->m_pRect, &prop, ::g_pDC, ::g_pDC);
			if(this->m_pParent->GetID() == 0)
			{
				//
				for(int i = 0; i < 8; ++i)
				{
					prop.clrFrame = pOP->m_VP[i].m_clrBack;
					pOP->m_VP[i].DrawTransparentRect(pOP->m_VP[i].m_pRect, &prop, ::g_pDC, ::g_pDC);
				}
			}
		}
	}

	if(m_nMode == CHN_INPUT)
	{
		//m_FMP.OnMouseMove(nFlags, point);
	}
	return m_VS.OnMouseMove(nFlags, point);
	//this->DrawBmp(this->m_pRect, &this->m_pBmp, NULL, NULL, REDRAW);
	//m_VS.m_pEdit->RedrawWindow();
	//m_VS.m_pEdit->UpdateWindow();
	//m_VS.m_pPlus->RedrawWindow();
	//m_VS.m_pPlus->UpdateWindow();
	//m_VS.m_pMinus->RedrawWindow();
	//m_VS.m_pMinus->UpdateWindow();
}

void UI_VolPanel::SetRect(const CRect *pRect)
{
	GraphicBase::SetRect(pRect);
	this->SetBmp(BMP_VOL_PANEL);
	CRect rc(pRect->left + LEN_PADDING, pRect->top + LEN_PADDING, pRect->right - LEN_PADDING, pRect->top + LEN_PADDING*3 + ((pRect->Width() - LEN_PADDING*3)/2)*4);
	//m_FMP.SetRect(&rc);
	CRect rcex(pRect->left, pRect->top + 2 * LEN_PADDING, pRect->right, pRect->bottom - 2 * LEN_PADDING);
	m_VS.SetRect(&rcex);
	m_rcOldSlider.SetRect(m_VS.GetRect()->left, m_VS.GetRect()->top, m_VS.GetRect()->right, m_VS.GetRect()->bottom);
}

void UI_VolPanel::SetID(int id)
{
	GraphicBase::SetID(id);
	//m_FMP.SetID(id);
	m_VS.SetID(id);
	m_VS.SetParentUI(this);
}

void UI_VolPanel::SetBaseWindow(CWnd *pWnd)
{
	GraphicBase::SetBaseWindow(pWnd);
	//m_FMP.SetBaseWindow(pWnd);
	m_VS.SetBaseWindow(pWnd);
}

void UI_VolPanel::SetMode(BYTE mode)
{
	m_nMode = mode;
	m_VS.SetMode(mode);
	//m_FMP.SetMode(mode);
	/*if(m_nMode == CHN_OUTPUT)
	{
		CRect rc;
		rc.SetRect(m_rcOldSlider.left, this->m_pRect->CenterPoint().y - m_rcOldSlider.Height()/2, m_rcOldSlider.right, this->m_pRect->CenterPoint().y + m_rcOldSlider.Height()/2);
		m_VS.SetRect(&rc);
	}
	else
	{
		m_VS.SetRect(&m_rcOldSlider);
	}*/
}

void UI_VolPanel::Save(const CString& path)
{
	//m_FMP.Save(path);
	m_VS.Save(path);
}

void UI_VolPanel::Load(const CString& path)
{
	//m_FMP.Load(path);
	m_VS.Load(path);
}

void UI_VolPanel::StepUp()
{
	m_VS.StepUp();
}

void UI_VolPanel::StepDown()
{
	m_VS.StepDown();
}

void UI_VolPanel::SetValue(bool bFlag)
{
	m_VS.SetValue(bFlag);
}